package it.agm.fanta.activity;

import it.agm.fanta.FantaserviceApp;
import it.agm.fanta.R;
import it.agm.fanta.adapter.FragmentListPagerAdapter;
import it.agm.fanta.fragment.PartitaLegaFragment;

import java.util.List;
import java.util.Vector;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

/**
 * 
 */
public class PartitaPagerActivity extends FragmentActivity  implements ActionBar.TabListener {
	
	private PagerAdapter pagerAdapter;
	private PartitaLegaFragment squadraCasaFragment;
	private PartitaLegaFragment squadraTrasfertaFragment;
	private ViewPager mViewPager;
	private Intent intent ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_match_pager);
		
		initializeSlides();
	}
		
	
	 private void initializeSlides() {

		 /** Getting fragment manager */
		FragmentManager fm = getSupportFragmentManager();
		List<Fragment> fragments = createSlideFragments();
		FantaserviceApp.partitaGiocata = false;
		pagerAdapter = new FragmentListPagerAdapter(fm, fragments);
		final ActionBar actionBar = getActionBar();
	
	    mViewPager = (ViewPager)findViewById(R.id.pager);
	    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

	    mViewPager.setAdapter(pagerAdapter);
	    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
	    actionBar.removeAllTabs();
	    for (int i = 0; i < pagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab().setText(pagerAdapter.getPageTitle(i)).setTabListener(this));
            
        }
	 }
	
	private List<Fragment> createSlideFragments() {
        final List<Fragment> fragments = new Vector<Fragment>();
    	squadraCasaFragment = new PartitaLegaFragment();
    	squadraCasaFragment.setTrasferta(false);
    	squadraCasaFragment.update();
    	squadraTrasfertaFragment = new PartitaLegaFragment();
    	squadraTrasfertaFragment.setTrasferta(true);
    	squadraTrasfertaFragment.update();
        fragments.add(squadraCasaFragment);
        fragments.add(squadraTrasfertaFragment);
        return fragments;
    }
	
	/**
	 * Metodo che gestisce la creazione del menu di options
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.partita, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	/**
	 * Metodo che gestisce lo switch tra le option del menu dell'actionBar
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// The action bar home/up action should open or close the drawer.
		// ActionBarDrawerToggle will take care of this.
		
		switch (item.getItemId()) {
		case R.id.action_back:
			finish();
			return true;
			
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	

	 @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }
}
